package org.atlantis.datasink.server.buildin;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class Test {
	
	public static void main(String[] args) throws Exception {
		ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
				Executors.newCachedThreadPool()));
		bootstrap.setPipelineFactory(new ChannelFactory());
		bootstrap.bind(new InetSocketAddress(5656));
	}

	private static class ChannelFactory implements ChannelPipelineFactory {

		@Override
		public ChannelPipeline getPipeline() throws Exception {
			return Channels.pipeline(new TestHandler());
		}
	}

	private static class TestHandler extends SimpleChannelUpstreamHandler {

		@Override
		public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
			System.out.println(ctx.getPipeline().getSink().getClass());
			System.out.println(ctx.getChannel().getClass().getSimpleName());
			System.out.println(ctx.getChannel().getRemoteAddress());
		}
	}
}
